package net.thxjj.mobileServer.util;


import io.jsonwebtoken.*;
import net.thxjj.sitermodel.MobileUser;

import java.util.Date;
import java.util.UUID;

public class JwtUtil {

    private static final String secretKey="siter jwt key";

    /**
     * 生成 JWT TOKEN
     * @param user 用户信息
     * @return String token
     */
    public static String createToken(MobileUser user){
        JwtBuilder jwtBuilder = Jwts.builder();
        String token = jwtBuilder
                //header
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                //payload
                .claim("userId", user.getUserId())
                .claim("userName", user.getUsername())
                .setSubject(user.getUsername())
                .setExpiration(new Date(System.currentTimeMillis() + 1000L * 60L * 60L * 24L))
                .setId(UUID.randomUUID().toString())
                //signature
                .signWith(SignatureAlgorithm.HS256, secretKey)
                .compact();
        return token;
    }

    /**
     * 生成 JWT TOKEN refreshToken
     * @param user 用户信息
     * @return String token
     */
    public static String createRefreshToken(MobileUser user){
        JwtBuilder jwtBuilder = Jwts.builder();
        String token = jwtBuilder
                //header
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                //payload
                .claim("userId", user.getUserId())
                .claim("userName", user.getUsername())
                .setSubject(user.getUsername())
                .setExpiration(new Date(System.currentTimeMillis() + 1000L * 60L * 60L * 24L * 30L))
                .setId(UUID.randomUUID().toString())
                //signature
                .signWith(SignatureAlgorithm.HS256, secretKey)
                .compact();
        return token;
    }


    /**
     * 解析 JWT TOKEN
     * @param token JWT TOKEN
     * @return User user
     */
    public static MobileUser parse(String token){
        JwtParser parser = Jwts.parser();
        Jws<Claims> claimsJws = parser.setSigningKey(secretKey).parseClaimsJws(token);
        Claims claims = claimsJws.getBody();
        System.out.println(claims.get("userId"));
        System.out.println(claims.get("userName"));
        System.out.println(claims.getId());
        System.out.println(claims.getSubject());
        System.out.println(claims.getExpiration());
        MobileUser user=new MobileUser();
        user.setUserId(Long.parseLong(claims.get("userId").toString()));
        user.setUsername((String)claims.get("userName"));
        return user;
    }

    public static void main(String[] args) {
        MobileUser user = new MobileUser();
        user.setUserId(1L);
        user.setUsername("张三");
        String token = createToken(user);
        System.out.println("token:"+token);

        String refreshToken = createRefreshToken(user);
        System.out.println("refreshToken:"+refreshToken);

        try {
            MobileUser u = parse("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjYzLCJ1c2VyTmFtZSI6IjEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDExIiwic3ViIjoiMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTEiLCJleHAiOjE2NzIxMzMzMjYsImp0aSI6ImJmYzBhYjM0LTFjMzAtNDk3ZC05OTJkLTM3MDNlOGI1MWM5MiJ9.GYuHCjxozY2dzTaABtr3fNh_J7fY-pHw4QtsIg0oArw");
            System.out.println(u);
        }catch (ExpiredJwtException e){
            e.printStackTrace();
        }

    }
}

